<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
//是否有网
var timerIn;
var timerCl;
var isconnected = false;

import Cookies from "js-cookie";
import { getMessageList, getName } from "@/BMViews/api/upload/index.js";
import { getUserProfile } from "@/api/system/user.js";
import { mapGetters } from "vuex";
export default {
  name: "App",
  metaInfo() {
    return {
      title:
        this.$store.state.settings.dynamicTitle &&
        this.$store.state.settings.title,
      titleTemplate: (title) => {
        return title
          ? `${title} - ${process.env.VUE_APP_TITLE}`
          : process.env.VUE_APP_TITLE;
      },
    };
  },
  data() {
    return {
      totalList: [],
      pageNum: 1,
      pageSize: 10,
      orderTotal: 0,
      // friendArr: [],
      // quoteName: "",
      // chatMessages: [],
      unreadMsg: 0,
      ipcRenderer: '',//electron应用消息提醒
    };
  },
  mounted() {
    // -------------electron应用消息提醒-------------
    // console.log(window);
    // -------------electron应用消息提醒-------------end

    window.$bus = this.$bus;
    window.$route = this.$route;
    if (localStorage.getItem("userId") && localStorage.getItem("token")) {
      this.$WebSocket.initialize();
      this.$WebSocket2.initialize();
      this.$store.dispatch('chat/getImLists')
      this.$store.dispatch('chat/getTaskLists')
      this.$store.dispatch('chat/getOrderLists')
    }

    // 全局监听网络变化
    window.addEventListener("online", () => {
      this.isNetwork = false;
      this.$bus.$emit("isNetwork", this.isNetwork);
      let that = this;
      let i = 0;

      //来网了（有网的情况下有三次重连的机会）
      //30秒执行一次
      if (typeof WebSocket != "undefined") {
        if (!isconnected) {
          this.$WebSocket.initialize();
          this.$WebSocket2.initialize();
          isconnected = true;
          if (!isconnected) {
            timerIn = setInterval(() => {
              this.$WebSocket.initialize();
              this.$WebSocket2.initialize();

              i++;
              if (i == 3) {
                window.clearInterval(timerIn);
              }
            }, 30000);
          }
        }
      } else {
        that.$message({
          showClose: true,
          message:
            "您的浏览器不支持Websocket通信协议，请使用Chrome或者Firefox浏览器！",
          type: "warning",
        });
      }
    });

    window.addEventListener("offline", () => {
      //断网了
      this.$message({
        showClose: true,
        message: "网络已断开",
        type: "warning",
      });
      this.isNetwork = true;
      this.$bus.$emit("isNetwork", this.isNetwork);
      // console.log(this.ws.readyState, "2222222222222222222222");
      isconnected = false;
      this.$WebSocket.onClose();
      this.$WebSocket2.onClose();
    });
  },
  created() {},
  methods: {},
  beforeDestroy() {
    this.$WebSocket.onClose();
    this.$WebSocket2.onClose();
  },
  computed: {
    ...mapGetters(["token", "avatar", "userId", "userType", "userInfo"]),
  },
};
</script>
